package com.tensquare.search.controller;

import com.tensquare.search.pojo.Article;
import com.tensquare.search.service.ArticleService;
import entity.PageResult;
import entity.Result;
import entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

/**
 * @description
 * @author: xu1
 **/

@RestController
@RequestMapping("/article")
@CrossOrigin
public class ArticleController {

    @Autowired
    private ArticleService articleService;

    @RequestMapping(method = RequestMethod.POST)
    public Result save(@RequestBody Article article) {
        articleService.save(article);
        return new Result(true, StatusCode.OK, "添加成功");
    }

    @RequestMapping(value="/{key}/{page}/{size}",method=RequestMethod.GET)
    public Result findByKey(@PathVariable String key,@PathVariable int page, @PathVariable int size){
        Page<Article> pageData = articleService.findByKey(key, page, size);
        return  new Result(true,StatusCode.OK,"查询成功",  new PageResult<Article>(pageData.getTotalElements(), pageData.getContent()) );
    }


}

//"hits": [
// {
// "_index": "tensquare_article",
// "_type": "article",
// "_id": "AW7ZzlPY_q1-rqFl8722",           这是人家es生成的id
// "_score": 1.0,
// "_source": {
// "id": null,         这个id是数据库的id
// "title": "JAVAarticle添加",
// "content": "ES--article测试",
// "state": null
// }
// }
// ]